package com.atguigu.stgy.app.service.impl;

import com.atguigu.stgy.app.service.ApartmentInfoService;
import com.atguigu.stgy.app.service.GraphInfoService;
import com.atguigu.stgy.app.service.RoomInfoService;
import com.atguigu.stgy.app.service.ViewAppointmentService;
import com.atguigu.stgy.common.interceptor.StgyAuthInterceptor;
import com.atguigu.stgy.model.entity.GraphInfo;
import com.atguigu.stgy.model.entity.ViewAppointment;
import com.atguigu.stgy.model.vo.ApartmentDetailVO;
import com.atguigu.stgy.model.vo.AppointmentDetailVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.atguigu.stgy.model.entity.ViewAppointment;
import com.atguigu.stgy.app.mapper.ViewAppointmentMapper;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment> implements ViewAppointmentService {
    @Resource
    GraphInfoService graphInfoService;
    @Resource
    ApartmentInfoService apartmentInfoService;
    @Resource
    RoomInfoService roomInfoService;
    @Override
    public List<ViewAppointment> listItem() {
        //以后设计表： 一般会参考三大范式 和 反三大范式设计表  ( 允许适当的冗余 减少表的联查  )
        List<ViewAppointment> appointments = this.list(Wrappers.lambdaQuery(ViewAppointment.class)
                .eq(ViewAppointment::getUserId, StgyAuthInterceptor.THREAD_LOCAL.get())
                .orderByDesc(ViewAppointment::getAppointmentTime));

        appointments.forEach(viewAppointment -> {
            //查询看房预约的公寓 图片列表
            viewAppointment.setGraphVoList(graphInfoService
                    .list(Wrappers.lambdaQuery(GraphInfo.class)
                        .eq(GraphInfo::getItemId ,
                                viewAppointment.getApartmentId()
                        )
                    )
            );
        });
        return appointments;
    }

    @Override
    public AppointmentDetailVO getDetailById(Long id) {
        //查询看房预约详情
        ViewAppointment appointment = this.getById(id);
        //查询公寓详情
        ApartmentDetailVO detailVO = apartmentInfoService.getDetailById(appointment.getApartmentId());
        //查询公寓的最低租金：  查询公寓的已发布的 未删除的 未签约的  价格最低的房间
        BigDecimal minRent = roomInfoService.getMinRentByApartmentId(appointment.getApartmentId());
        //组装数据
        AppointmentDetailVO vo = new AppointmentDetailVO();
        BeanUtils.copyProperties(appointment , vo);
        vo.setApartmentItemVo(detailVO);

        vo.setMinRent(minRent);
        return vo;
    }
}
